<template>
  <div id="app">
    <top></top>
    <router-view v-if="isRouteAlive"></router-view>
    <foot></foot>
    <websocket-worker></websocket-worker>
    <player></player>
  </div>
</template>

<script>

import {getUser, clearUser} from './util/userUtils'
import {apiContext} from "./service/getData"
import top from './components/top'
import foot from './components/foot'
import player from './components/player'
import WebsocketWorker from './components/websocketWorker'
import Bus from './util/bus'

export default {
  name: 'app',
  data(){
    return{
      isRouteAlive: true
    }
  },
  provide() {
    return {
      reload: this.reload
    }
  },
  methods:{
    reload() {
      this.isRouteAlive = false;
      this.$nextTick(function () {
        this.isRouteAlive = true;
      })
    },
    checkToken() {
      if (getUser() !== null) {
        let user = getUser();
        if (user.userToken === '' || user.userToken === undefined || user.userToken === null ) {
          clearUser();
          Bus.$emit("refresh-user", user);
          return ;
        }
        let _this = this;
        this.$http.post(apiContext + "checkToken", {}, {
          emulateJSON: true,
          headers: {
            'Content-Type': 'x-www-form-urlencoded',
            'userId': user.id,
            'userToken': user.userToken
          }
        }).then(res => {
          console.log(res);
          if (res.body.status !== 200) {
            clearUser();
          } else {
          }
        }).catch(res => {
          console.log(res)
          this.$message({
            message: '账号已在异地登录了，请重新登录',
            type: 'info'
          });
          clearUser();
          Bus.$emit("refresh-user", user);
        })
      } else {

      }
    }
  },
  mounted() {
    this.checkToken();

  },
  components: {
    WebsocketWorker,
    top,
    foot, player
  }
}
</script>

<style>
@import './style/common.css';
#app {
  float: left;
  width: 100%;
}
</style>
